import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/util/ext_utils.dart';

import '../../../config/global_configs.dart';
import '../../../net/response/food/week_menu_list_data.dart';
import '../../../res/colors.dart';
import '../../../res/gaps.dart';
import '../../../viewmodel/food/new_week/new_choose_week_submit_view_model.dart';
import '../../../viewmodel/food/new_week/new_regular_week_submit_view_model.dart';
import '../../../widgets/bottom_input_widget.dart';

class NewChooseWeekSubmitPage extends BaseStatefulMvvmWidget{
  WeekMenuListData menuData;
  final String oldId;
  final String sitId;
  final String time;
  final String orderModelType;
  final String useType;
  NewChooseWeekSubmitPage(
      {required this.menuData,
        required this.oldId,
        required this.sitId,
        required this.time,
        required this.orderModelType,
        required this.useType});
  @override
  State<StatefulWidget> createState() => _NewChooseWeekSubmitState();
}

class _NewChooseWeekSubmitState extends BaseMvvmState<NewChooseWeekSubmitViewModel,NewChooseWeekSubmitPage>{
  @override
  Widget buildLoadingContentView() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        ListView(
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                CommonText('已点项目', textColor: Colors.black, textSize: 18),
                Container(
                  height: 10,
                ),
                ListView.separated(
                  itemBuilder: (context, index) {
                    var item = viewModel.selectedMealList[index];
                    return selectMealItem(item);
                  },
                  separatorBuilder: (context, index) {
                    return Gaps.dividerLine;
                  },
                  itemCount: viewModel.selectedMealList.length,
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                )
              ],
            ).addToContainer(
                margin: EdgeInsets.symmetric(horizontal: 16, vertical: 10),
                padding: EdgeInsets.all(10),
                decoration: BoxDecoration(
                    color: Colors.white, borderRadius: 10.borderRadius)),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                CommonText('备注信息', textColor: Colors.black, textSize: 18),
                Container(
                  height: 10,
                ),
                CommonText(viewModel.remark, hintText: '输入备注信息')
                    .addToContainer(
                    decoration: BoxDecoration(
                        color: Colours.bg_color,
                        borderRadius: 10.borderRadius),
                    padding: EdgeInsets.all(8),
                    width: MediaQuery.of(context).size.width,
                    constraints: BoxConstraints(minHeight: 100))
                    .onTap(() {
                  showBottomInput(context,
                      title: "备注信息",
                      content: viewModel.remark, onSubmit: (str) {
                        setState(() {
                          viewModel.remark = str;
                        });
                      });
                })
              ],
            ).addToContainer(
                margin: EdgeInsets.symmetric(horizontal: 16, vertical: 10),
                padding: EdgeInsets.all(10),
                decoration: BoxDecoration(
                    color: Colors.white, borderRadius: 10.borderRadius)),
          ],
        ).setWeight(1),
        Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            if(GlobalConfigs.showPayPrice)
              CommonText('合计 '),
            GlobalConfigs.showPayPrice?
            CommonText("¥${viewModel.totalPrice.d-viewModel.oldPrice.d}",
                textSize: 18):CommonText(""),
            Container(width: 16,),
            TextButton(
              onPressed: () {
                viewModel.submitRequest();
              },
              child: CommonText('确认提交', textColor: Colors.white),
              style: ButtonStyle(
                  backgroundColor:
                  WidgetStateProperty.all(Colours.foodSelectColor),
                  shape: WidgetStateProperty.all(StadiumBorder()),
                  padding: WidgetStateProperty.all(EdgeInsets.symmetric(horizontal: 18))
              ),
            ),
            Container(width: 16,),
          ],
        ).addToContainer(height: 49, color: Colors.white)
      ],
    );
  }

  Widget selectMealItem(GoodsInfo info) {
    var paidList = <GoodsList>[];
    var selectList = <GoodsList>[];
    var selectPrice = 0.0;
    var paidPrice = 0.0;
    for (var item in info.chooseData) {
      selectList.addAll(item.selectList);
      paidList.addAll(item.paidList);
      selectPrice += item.selectPrice;
      paidPrice += item.paidPrice;
    }
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          height: 8,
        ),
        Row(
          children: [
            CommonText(
                "${info.weekDate?.toDateStr([
                  'm',
                  '月',
                  'd',
                  '日'
                ])}  ${info.timeTypeId?.timeTypeStr()}",
                textSize: 17,
                textColor: Colors.grey[700]!,
                fontWeight: FontWeight.bold)
                .setWeight(1),
            CommonText("¥${selectPrice - paidPrice}",
                textSize: 17, textColor: Colors.red),
          ],
        ),
        Container(
          height: 15,
        ),
        CommonText(
            selectList.map((e) => "${e.goodsName}x${e.number}").join("、"),
            textColor: Colors.grey[800]!,
            textSize: 17),
        Container(
          height: 15,
        ),
        if (paidList.isNotEmpty) ...[
          CommonText(
            '已结算',
            textColor: Colors.grey,
          ),
          Container(
            height: 15,
          ),
          ...paidList
              .map((e) => Row(
            children: [
              Container(
                width: 40,
              ),
              CommonText(e.goodsName, textColor: Colors.grey)
                  .setWeight(2),
              CommonText("x${e.oldNumber}",
                  textColor: Colors.grey,
                  textAlign: TextAlign.center)
                  .setWeight(1),
              CommonText("¥${e.price}",
                  textColor: Colors.grey, textAlign: TextAlign.end)
                  .setWeight(1),
            ],
          ))
              .toList(),
          Container(
            height: 15,
          ),
        ]
      ],
    );
  }


  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      backgroundColor: Colours.bg_color,
      appBar: AppBar(
        title: Text("提交订单"),
      ),
      body: loadingContentWidget,
    );
  }

  @override
  NewChooseWeekSubmitViewModel createViewModel() {
    return NewChooseWeekSubmitViewModel(context,
        menuData: widget.menuData,
        oldId: widget.oldId,
        sitId: widget.sitId,
        time: widget.time,
        orderModelType: widget.orderModelType,
        useType: widget.useType);
  }

  @override
  void onRetryClick() {}

}